﻿using Microsoft.AzureAD.XPrize.TwittOracle.Algorithms;
using Microsoft.AzureAD.XPrize.TwittOracle.DataCollection;
using Microsoft.AzureAD.XPrize.TwittOracle.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TweetSharp;

namespace Microsoft.AzureAD.XPrize.TwittOracle
{
    class Program
    {
        private static TwitterService service;

        private enum Options
        {
            CollectVerifiedData = 1,
            CollectInvalidData,
            GenerateModel,
            GiveAnInput,
        };

        static void Main(string[] args)
        {
            Console.WriteLine("1. Verified data collection");
            Console.WriteLine("2. Invalid data collection");
            Console.WriteLine("3. Generate model");
            Console.WriteLine("4. Test using Model");
            Console.WriteLine("Enter the operation you want to perform: ");
            string value = Console.ReadLine();
            int intValue;
            if (int.TryParse(value, out intValue))
            {
                // Setup
                service = new TwitterService(
                    Resources.consumerKey,
                    Resources.consumerSecret,
                    Resources.accessToken,
                    Resources.accessSecret);

                Options option = (Options)intValue;

                switch (option)
                {
                    case Options.CollectVerifiedData:
                        // Populate the file Data\accounts_valid.txt with valid screenNames
                        VerifiedAccountExtractor vae = new VerifiedAccountExtractor(service);
                        vae.getValidAccounts();
                        break;

                    case Options.CollectInvalidData:
                        InvalidAccountsExtractor iae = new InvalidAccountsExtractor(service);
                        iae.getInvalidAccounts();
                        break;

                    case Options.GenerateModel:
                        Perceptron p = new Perceptron();
                        p.Generate();
                        break;

                    case Options.GiveAnInput:
                        string input = "notExit";
                        while (!input.Equals("exit", StringComparison.OrdinalIgnoreCase))
                        {
                            Console.Write("Enter a twitter handle (type exit to quit): ");
                            input = Console.ReadLine();
                            bool? isValid = FindIfValid(input);
                            if (isValid == null)
                                Console.WriteLine("Twitter Handle invalid");
                            else
                                Console.WriteLine("Prediction: " + isValid);
                        }
                        break;

                    default:
                        Console.WriteLine("Invalid Input");
                        break;
                }
            }
            else
            {
                Console.WriteLine("Invalid Input.");
            }

            Console.WriteLine("Press any key to exit.");
            Console.Read();
        }

        private static bool? FindIfValid(string handle)
        {
            var profile = service.GetUserProfileFor(handle);
            if (profile == null)
                return null;
            var account = TwitterAccount.GetSingle(profile);
            Perceptron p = new Perceptron();
            var output = p.Test(account);

            return output.IsValid;
        }
    }
}
